package com.example.link;

// 链表反转 - 迭代方式
public class Iterator {

    private static class LinkNode {

        int val;
        LinkNode next;

        public LinkNode(int val, LinkNode next) {
            this.val = val;
            this.next = next;
        }
    }

    // 迭代
    private static LinkNode iterator(LinkNode head) {

        LinkNode prev = null, next;
        LinkNode curr = head;

        while (curr != null) {
            next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }

        return prev;
    }


    public static void main(String[] args) {

        LinkNode node5 = new LinkNode(5, null);
        LinkNode node4 = new LinkNode(4, node5);
        LinkNode node3 = new LinkNode(3, node4);
        LinkNode node2 = new LinkNode(2, node3);
        LinkNode node1 = new LinkNode(1, node2);

        LinkNode prev = iterator(node1);
        System.out.println(prev);



    }

}
